package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.linkbeans.SysRolePermission;

import com.edu.app.constant.ErrorMessage;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.linkservices.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/api")
@RestController
public class SysRolePermissionController {

    @Autowired
    SysRolePermissionService sysRolePermissionService;

    /*
     * 为角色添加权限
     * http://localhost:8080/api/addRolePermission?role_id=1&permission_id=1
     * */
    @PostMapping(value = "/addRolePermission")
    public ModelMap addUserRole(@RequestParam Integer role_id, @RequestParam Integer permission_id){
        ModelMap m = new ModelMap();

        try{
            Integer r = sysRolePermissionService.add(new SysRolePermission(role_id, permission_id));
            if(r > 0){
                m.put(ErrorMessage.ErrorCode, ErrorMessage.SucceedCode);
                m.put(SysMessage.SysMessage, SysMessage.AddRolePermissionSucceed);
            }else{
                m.put(ErrorMessage.ErrorCode, ErrorMessage.FailedCode);
                m.put(SysMessage.SysMessage, SysMessage.AddRolePermissionFailed);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            m.put(ErrorMessage.ErrorCode, ErrorMessage.FailedCode);
            m.put(ErrorMessage.ErrorMessage, ErrorMessage.RolePermissionAlreadyExist);
        }catch (Exception e){
            m.put(ErrorMessage.ErrorCode, ErrorMessage.FailedCode);
            m.put(ErrorMessage.ErrorMessage, e.getMessage());
        }

        return m;
    }

    /*
     * 删除角色权限
     * http://localhost:8080/api/deleteRolePermission?user_id=1&role_id=1
     * */
    @PostMapping(value = "/deleteRolePermission")
    public ModelMap deleteRolePermission(@RequestParam Integer role_id, @RequestParam Integer permission_id){

        ModelMap m = new ModelMap();
        Integer id = sysRolePermissionService.getRolePermissionId(role_id, permission_id);

        if(id > 0){
            try{
                Integer d = sysRolePermissionService.delete(id);
                if(d > 0){
                    m.put(ErrorMessage.ErrorCode, ErrorMessage.SucceedCode);
                    m.put(SysMessage.SysMessage, SysMessage.DeleteRolePermissionSucceed);
                }else{
                    m.put(ErrorMessage.ErrorCode, ErrorMessage.FailedCode);
                    m.put(SysMessage.SysMessage, SysMessage.DeleteRolePermissionFailed);
                }
            }catch (Exception e){
                m.put(ErrorMessage.ErrorCode, ErrorMessage.FailedCode);
                m.put(ErrorMessage.ErrorMessage, e.getMessage());
            }
        }

        return m;
    }
}
